package com.huiminpay.transaction.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.huiminpay.transaction.api.IPayOrderService;
import com.huiminpay.transaction.convert.PayOrderConvert;
import com.huiminpay.transaction.dto.OrderResultDTO;
import com.huiminpay.transaction.entity.PayOrder;
import com.huiminpay.transaction.mapper.PayOrderMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;


@Service
public class IPayOrderServiceImpl implements IPayOrderService {

    @Autowired
    PayOrderMapper payOrderMapper;


    @Override
    public OrderResultDTO queryByOutTradeNo(String out_trade_no) {
        QueryWrapper<PayOrder> qw = new QueryWrapper<>();
        qw.lambda().eq(PayOrder::getOutTradeNo,out_trade_no);

        PayOrder payOrder = payOrderMapper.selectOne(qw);


        return PayOrderConvert.INSTANCE.entity2dto(payOrder);
    }

    @Override
    public void updateTradeState(OrderResultDTO orderResultDTO) {
        UpdateWrapper<PayOrder> qw = new UpdateWrapper<>();
        qw.lambda().eq(PayOrder::getOutTradeNo,orderResultDTO.getOutTradeNo());

        PayOrder payOrder = PayOrderConvert.INSTANCE.dto2entity(orderResultDTO);

        payOrderMapper.update(payOrder,qw);
    }
}
